package com.parkcar;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import HttpUtils.HttpHistoryRequest;
import Modle.HistoryBean;
import adapter.HistoryAdapter;
import ui.RefreshLayout;
import util.BaseActivity;
import util.BaseTools;
import util.Constrants;

/**
 * Created by Administrator on 2016/2/19.
 */
public class HistoryActivity extends BaseActivity{
    private ListView mListView;
    private TextView ero_title,ero_content,ero_click;
    private RelativeLayout ero_ll;
    private HttpHistoryRequest historyRequest;
    private int pageNum=0;
    private HistoryAdapter mAdapter;
    private RefreshLayout mRefrash;
    private ReChargeListener reChargeListener;
    @Override
    protected void setUpui(Bundle arg0) {
        setContentView(R.layout.activity_base_list);
        setTitle("停车记录");
        reChargeListener = new ReChargeListener();
        registerReceiver(reChargeListener, new IntentFilter(Constrants.RECHARG_SUCCESS));
        initView();
    }
    private void initView(){
        mListView = (ListView) findViewById(R.id.mListView);
        ero_title = (TextView) findViewById(R.id.ero_title);
        ero_content = (TextView) findViewById(R.id.ero_content);
        ero_click = (TextView) findViewById(R.id.ero_click);
        ero_ll = (RelativeLayout) findViewById(R.id.ero_ll);
        mRefrash = (RefreshLayout) findViewById(R.id.pull_ly);
        ero_click.setVisibility(View.GONE);
        ero_title.setText("您还没有停车记录哦");
        ero_content.setText("快去使用app停车，更有预约车位功能哦");
        mAdapter = new HistoryAdapter(this);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if(mAdapter!=null&&mAdapter.getCount()>i){
                    Bundle mbundle = new Bundle();
                    mbundle.putString("order_no",((HistoryBean)mAdapter.getItem(i)).getOrder_no());
                    showActivity(ParkDetailActivity.class,mbundle);
                }
            }
        });
        mRefrash.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageNum=0;
                getData();
            }
        });
        mRefrash.setOnLoadListener(new RefreshLayout.OnLoadListener() {
            @Override
            public void onLoad() {
                pageNum++;
                getData();
            }
        });
        getData();
    }
    @Override
    protected void updateUI(boolean isLogin) {

    }
    private void getData(){
        historyRequest = new HttpHistoryRequest(this,0);
        historyRequest.setPageNum(pageNum);
        historyRequest.setmIHttpClinetListener(this);
        historyRequest.doRequest();
    }

    @Override
    public void httpSuccessListener(Bundle successMsg) {
        super.httpSuccessListener(successMsg);
        mRefrash.setRefreshing(false);
        mRefrash.setLoading(false);
        if(mBaseResult==null){return;}
        if(historyRequest!=null&&action.equalsIgnoreCase(historyRequest.getAction())){
            if(mBaseResult.getStatus().getRetCode()==0){
                List<HistoryBean> mList = mBaseResult.toArray(HistoryBean.class,"list");
                if(pageNum==0){
                    if(BaseTools.isValueEmpty(mList)){
                       ero_ll.setVisibility(View.VISIBLE);
                    }else{
                        ero_ll.setVisibility(View.GONE);
                        mAdapter.setData(mList);
                    }
                }else{
                    if(!BaseTools.isValueEmpty(mList)){
                        ero_ll.setVisibility(View.GONE);
                        mAdapter.setData(mList);
                    }
                }
            }else{
                toast(mBaseResult.getStatus().getMsg());}
        }
    }

    class ReChargeListener extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            pageNum=0;
            getData();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(reChargeListener);
    }
}
